from django.db import models
from user.models import Userinfo, StudentClass
from datetime import timedelta

"""
1.小题目
2.小题目的选项
3.试卷大标题
4.空白试卷
5.学生答卷
6.学生答案
"""


# 科目
class Subject(models.Model):
    name = models.CharField(max_length=128, unique=True, null=False, verbose_name='科目名称')
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        db_table = 'subject'
        verbose_name_plural = '学科科目'

    def __str__(self):
        return self.name

    def time(self):
        return self.create_time.strftime("%Y-%m-%d %H:%M:%S")

    def title_count(self):
        return self.titleOfSubject.count()

    def paper_count(self):
        return self.paperOfSubject.count()


# 小题目的选项(A.xxx B.xxx C.xxx D.xxx)
class Option(models.Model):
    prefix = models.CharField(max_length=64, null=True, blank=True, verbose_name='答案选项头')
    content = models.TextField(null=False, verbose_name='选项内容')
    option = models.ForeignKey(to='Title', related_name='optionItem', on_delete=models.CASCADE, verbose_name='所属题目')

    class Meta:
        db_table = 'option'
        verbose_name_plural = '题目选项'

    def __str__(self):
        return self.content[:10]


# 小题目
class Title(models.Model):
    show_title_type = (
        (0, '单选题'),
        (1, '多选题'),
        (2, '判断题'),
        (3, '简答题'),
    )
    theme = models.TextField(null=False, verbose_name='题目标题')
    right_answer = models.TextField(null=False, verbose_name='题目正确答案')  # 单选:'A'  多选:'ABC'  判断:'0'  '1'  简答题:'答案文本'
    title_type = models.SmallIntegerField(choices=show_title_type, null=False, verbose_name='题目类型')
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    title_score = models.DecimalField(decimal_places=2, max_digits=6, verbose_name='题目分数', null=False)
    subject = models.ForeignKey(to="Subject", on_delete=models.SET_NULL, null=True, db_constraint=False,
                                related_name='titleOfSubject',
                                verbose_name='小题关联科目')

    class Meta:
        db_table = 'title'
        verbose_name_plural = '小题目'

    def __str__(self):
        return self.theme[:10]

    def subject_name(self):
        return self.subject.name

    def type_name(self):
        return self.get_title_type_display()

    def time(self):
        return self.create_time.strftime('%Y-%m-%d %H:%M:%S')

    def blank(self):
        if self.title_type == 1:
            return []
        else:
            return ''


# 试卷的大标题(选择、判断、简答)
class Topic(models.Model):
    content = models.TextField(null=False, verbose_name='大标题内容')
    titles = models.ManyToManyField(to="Title", verbose_name='大标题下的小题')
    paper = models.ForeignKey(to="Paper", related_name='topicItem', on_delete=models.CASCADE, db_constraint=False,
                              null=True, verbose_name='所属试卷')

    class Meta:
        db_table = 'topic'
        verbose_name_plural = '试卷大标题'

    def __str__(self):
        return self.content[:10]


# 空白试卷
class Paper(models.Model):
    name = models.CharField(max_length=256, verbose_name='试卷名称')
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True, )
    paper_score = models.DecimalField(decimal_places=2, max_digits=10)
    subject = models.ForeignKey(to="Subject", on_delete=models.SET_NULL, null=True, db_constraint=False,
                                related_name='paperOfSubject',
                                verbose_name='试卷关联科目')

    class Meta:
        db_table = 'paper'
        verbose_name_plural = '空白试卷'

    def __str__(self):
        return self.name

    def time(self):
        return self.create_time.strftime('%Y-%m-%d %H:%M:%S')

    def subject_name(self):
        return self.subject.name


class Task(models.Model):
    task_types = (
        (0, '定时长考试'),
        (1, '不定长考试'),
    )
    name = models.CharField(max_length=128, null=False, verbose_name='任务名称', default='考试任务')
    task_type = models.SmallIntegerField(choices=task_types, default=0, verbose_name='考试类型')
    create_time = models.DateTimeField(auto_now_add=True)
    start_time = models.DateTimeField(null=True, blank=True, verbose_name='考试开始时间')
    duration = models.IntegerField(default=0, verbose_name='考试时长分钟', null=True)
    paper = models.ForeignKey(Paper, on_delete=models.SET_NULL, null=True, db_constraint=False, verbose_name='任务关联试卷')
    to_class = models.ForeignKey(StudentClass, on_delete=models.SET_NULL, null=True, db_constraint=False,
                                 verbose_name='任务关联班级')

    class Meta:
        db_table = 'task'
        verbose_name_plural = '考试任务'

    def __str__(self):
        return self.name

    def show_type(self):
        return self.get_task_type_display()

    def begin_time(self):
        return self.start_time.strftime('%Y-%m-%d %H:%M:%S')

    def class_name(self):
        return self.to_class.name


# 学生个人答卷
class StudentPaper(models.Model):
    task_types = (
        (0, '定时长考试'),
        (1, '不定长考试'),
    )
    status_type = (
        (0, '考试未开始'),
        (1, '正在考试中'),
        (2, '考试已结束'),
        (3, '已批改完成'),
    )
    task_type = models.SmallIntegerField(choices=task_types, default=0, verbose_name='考试类型')
    start_time = models.DateTimeField(null=True, blank=True, verbose_name='考试开始时间')
    duration = models.IntegerField(default=0, verbose_name='考试用时')
    status = models.SmallIntegerField(choices=status_type, default=0, verbose_name='考试进行状态')
    task = models.ForeignKey(to="Task", on_delete=models.CASCADE, related_name='studentOfTask', verbose_name='学生答卷关联任务')
    student = models.ForeignKey(Userinfo, verbose_name='关联学生', on_delete=models.SET_NULL, null=True,
                                db_constraint=False, related_name='studentPaper')  # 没有约束 可以为空
    paper = models.ForeignKey(to="Paper", on_delete=models.SET_NULL, null=True, db_constraint=False,
                              verbose_name='关联空白试卷')  # 没有约束 可以为空
    sys_score = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='系统评分客观题')
    hand_score = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='人工评分主观题')
    final_score = models.DecimalField(max_digits=10, decimal_places=2, default=0.00, verbose_name='最终得分')

    class Meta:
        db_table = 'studentpaper'  # 指定表名
        verbose_name_plural = '学生个人答卷'

    def __str__(self):
        return f"{self.student.username}的答卷:{self.paper.name}"

    def student_name(self):
        return self.student.username

    def begin_time(self):
        return self.start_time.strftime('%Y-%m-%d %H:%M:%S')

    def show_status(self):
        return self.get_status_display()

    def show_duration(self):
        return str(self.duration) + 'min'

    def task_name(self):
        return self.task.name

    def deadline(self):
        return (self.start_time + timedelta(minutes=self.task.duration)).strftime('%Y-%m-%d %H:%M:%S')

    def type_name(self):
        return self.get_task_type_display()

    def task_duration(self):
        return self.task.duration


class StudentAnswer(models.Model):
    right_type = (
        (0, '暂未批改'),
        (1, '正确'),
        (2, '部分正确'),
        (3, '错误'),
    )
    student_paper = models.ForeignKey(to="StudentPaper", verbose_name='关联学生答卷', on_delete=models.CASCADE, )
    content = models.TextField(verbose_name='学生答案内容', null=True, blank=True)
    title = models.ForeignKey(to="Title", verbose_name='关联题目', related_name='studentAnswer', on_delete=models.SET_NULL,
                              db_constraint=False,
                              null=True)
    student_score = models.DecimalField(decimal_places=2, max_digits=6, verbose_name='学生得分', null=True, blank=True)
    is_right = models.SmallIntegerField(choices=right_type, default=0, verbose_name='答案批改状态')

    class Meta:
        db_table = 'studentanswer'
        verbose_name_plural = '学生答案'

    def __str__(self):
        return self.content[:10]

    def right_type(self):
        return self.get_is_right_display()
